﻿using PrintDemo.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace PrintDemo
{
    public partial class Form2 : Form
    {
        /// <summary>
        /// 每页记录数
        /// </summary>
        private int pageSize = 3;

        /// <summary>
        /// 总记录数
        /// </summary>
        private int recordCount = 0;

        /// <summary>
        /// 总页数
        /// </summary>
        private int pageCount = 0;

        /// <summary>
        /// 当前页
        /// </summary>
        private int currentPage = 1;
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            txtCurrentPage.Text = "0";
            loadData();
        }
        /// <summary>
        /// 加载数据
        /// </summary>
        private void loadData()
        {
            dataGridView1.DataSource = DBUtils.GetListTable(currentPage, pageSize, ref recordCount);         
            pageCount = (recordCount + pageSize - 1) / pageSize;
            if (pageCount == 0)
            {
                lblRecordCount.Text = "0";
                txtCurrentPage.Text = "0";
                lblTotal.Text = "/0页";
            }
            else
            {                
                lblRecordCount.Text = "总记录数:" + recordCount;
                txtCurrentPage.Text = currentPage.ToString();
                lblTotal.Text = "/" + pageCount + "页";
            }


        }
        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnFirst_Click(object sender, EventArgs e)
        {
            currentPage = 1;
            loadData();
        }
        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (currentPage < pageCount)
            {
                currentPage++;
                loadData();
            }
            else
            {
                MessageBox.Show("当前已经是最后一页");
            }
        }
        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLast_Click(object sender, EventArgs e)
        {
            if (currentPage == 1)
            {
                MessageBox.Show("当前已经是第一页");
            }
            else
            {
                currentPage--;
                loadData();

            }
        }
        /// <summary>
        /// 末页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnEnd_Click(object sender, EventArgs e)
        {
            currentPage = pageCount;
            loadData();
        }
        /// <summary>
        /// 跳转页码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnGoTo_Click(object sender, EventArgs e)
        {
            if (!isIntergerNonZero(txtCurrentPage.Text.Trim()))
            {
                MessageBox.Show("跳转页码必须为正整数");
            }
            else
            {               
               int page = Convert.ToInt32(txtCurrentPage.Text.Trim());
                if(page<=pageCount)
                {
                    currentPage = page;
                    loadData();
                }
                else
                {
                    MessageBox.Show("跳转页面不能大于总页数");
                }

            }
        }
        /// <summary>
        /// 校验是否为正整数
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static bool isIntergerNonZero(string str)
        {
            return string.IsNullOrWhiteSpace(str) ? false : Regex.IsMatch(str, @"^[1-9]\d*$");
        }
    }
}
